package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.util.QueryObject;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//部门控制器
@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //查询    /department/list.do
    @RequestMapping("/list")
    @RequiresPermissions(value = {"department:list", "部门列表"} , logical = Logical.OR)
    public String list(Model model , @ModelAttribute("qo")QueryObject qo){
        // 共享分页数据给页面
        PageInfo<Department> pageResult = departmentService.query(qo);
        model.addAttribute("pageResult",pageResult);
        return "department/list";
    }

    //删除    /department/delete.do?id=?
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        departmentService.deleteById(id);
        return new JsonResult();
    }

    //修改或者新增
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department){
        if (department.getId() != null) {
            departmentService.update(department);
        }else {
            departmentService.insert(department);
        }
        return new JsonResult();
    }

}
